import { getCodeImg } from '@/api/login/login'
import { toast } from '@/utils/common'
import { AppType } from '@/utils/constant.js'
import { getWxAppId } from '@/api/sr/wechat'
export default {
    data() {
        return {
			title: '欢迎回来！', //填写logo或者app名称，也可以用：欢迎回来，看您需求
			phone: '', //手机号码
			password: '',
			code: '', //验证码
            codeUrl: "",
            captchaEnabled:true,
        }
    },
    computed: {
        appMp(){
            return uni.getStorageSync('appType')== AppType.MP
        },
		appMINI() {
			return uni.getStorageSync('appType') == AppType.MINI
		}
    },
    created() {
        this.getCode()
    },
    methods: {
        // 获取图形验证码
        getCode() {
            getCodeImg().then(res => {
                this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
                if (this.captchaEnabled) {
                    this.codeUrl = 'data:image/gif;base64,' + res.data.img
                    this.uuid = res.data.uuid
                }
            }).catch((err) => {
            });
        },
        submit() {
			var that = this;
			if (!that.phone) {
				uni.showToast({ title: '请输入手机号', icon: 'none' });
				return;
			}
			if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(that.phone)) {
				uni.showToast({ title: '请输入正确手机号', icon: 'none' });
				return;
			}
			if (!that.password) {
				uni.showToast({ title: '请输入密码', icon: 'none' });
				return;
			}
			if (!that.code) {
				uni.showToast({ title: '请输入验证码', icon: 'none' });
				return;
			}
			const params = {
			    phoneNumber: that.phone,
			    password: that.password,
				code: that.code,
				uuid: this.uuid
			}
			this.$store.dispatch('Login', params).then(() => {
				this.$modal.closeLoading()
				this.loginSuccess()
			}).catch(() => {
				if (this.captchaEnabled) {
					this.getCode()
				}
			})
        },
        // 登录成功后，处理函数
        loginSuccess(result) {
            // 设置用户信息
            this.$store.dispatch('GetInfo').then(res => {
                this.$tab.reLaunch('/pages/user/profile')
            }).catch((err) => {
            });
        },
        toWeChatLogin(){
            if (!this.appMp) {
                toast('非微信环境无法登录！');
                return;
            } else{
                console.info('isLogin',uni.getStorageSync('isLogin') )
                if(uni.getStorageSync('isLogin')){
                    return;
                }
                uni.setStorageSync('isLogin', true)
                this.weChatLogin();
            }
        },
        weChatLogin(){
            const url = window.location.href
            if (url.indexOf('code') > -1) {
                uni.setStorageSync('isLogin', false)
                if (getApp().globalData.isLanding) return
                getApp().globalData.isLanding = true
                const code = url.split('code=')[1].split("&")[0];
                console.info(code)
                this.$store.dispatch('XcxLogin', code).then(() => {
                    this.$modal.closeLoading()
                    // 设置用户信息
                    this.$store.dispatch('GetInfo').then(res => {
                        this.$tab.reLaunch('/pages/user/profile')
                    }).catch(() => {
                    })
                }).catch(() => {
                    window.location.href = window.location.origin +'#/pages/login/login'
                })
            } else {
                getWxAppId().then(res => {
                    const redirectUrl = window.location.href;
                    let param = 'appid=' + res.data;
                    param += '&response_type=code'
                    param += '&scope=snsapi_base'
                    param += '&redirect_uri=' + encodeURIComponent(redirectUrl)
                    param += '&state=bihuashop#wechat_redirect'
                    window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?' + param
                }).catch(err => {
                    window.location.href = window.location.origin +'#/pages/login/login'
                })
            }
        },
        goPage(url) {
            this.$tab.navigateTo(url)
        }
    },
};